package cn.school.course.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.school.common.exception.ApiException;
import cn.school.common.utils.CourseConstants;
import cn.school.course.dao.CoursePlanConflictDao;
import cn.school.course.entity.CourseConflict;
import cn.school.course.entity.CoursePlan;
import cn.school.course.entity.CoursePlanConflict;
import cn.school.course.service.CourseConflictService;
import cn.school.course.service.CoursePlanConflictService;
import cn.school.course.service.CourseService;
import cn.school.sys.entity.SysConfig;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @className: 排课计划-冲突情况ServiceImpl
 * @author: fangjin.tao
 * @date: 2023/1/5
 **/
@Service
public class CoursePlanConflictServiceImpl extends ServiceImpl<CoursePlanConflictDao, CoursePlanConflict> implements CoursePlanConflictService {
	@Autowired
	private CourseService courseService;
	@Autowired
	private CourseConflictService courseConflictService;
	
	@Override
	@Transactional
	public void saveTransCourseConflict(SysConfig coplConflictEnable, CoursePlan coursePlan) {
		LambdaQueryWrapper<CoursePlanConflict> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(CoursePlanConflict::getCoplId, coursePlan.getId());
		queryWrapper.eq(CoursePlanConflict::getCpcoIsGennerCourse, 0);
		queryWrapper.gt(CoursePlanConflict::getCourEndTime, Calendar.getInstance().getTime());
		if ("0".equals(coplConflictEnable.getParamValue())) {
			// 不允许冲突情况时，必须是无冲突的情况
			queryWrapper.eq(CoursePlanConflict::getCpcoIsConflict, 0);
		}
		List<CoursePlanConflict> coursePlanConflictList = this.list(queryWrapper);
		if (coursePlanConflictList != null && coursePlanConflictList.size() != 0) {
			CourseConflict tempCourse = null;
			for (CoursePlanConflict coursePlanConflict : coursePlanConflictList) {
				tempCourse = new CourseConflict();
				BeanUtil.copyProperties(coursePlanConflict, tempCourse);
				tempCourse.setCpcoId(coursePlanConflict.getId());
				courseConflictService.save(tempCourse);
				
			}
			this.baseMapper.updateCpcoIsGennerCourse1(coursePlan.getId());
		}
	}
	
	@Override
	@Transactional
	public void saveCpcoConflictProcess(Date time, Date tetiTermEndDate, String conflictType, List<String> keyList) {
		String groupByKey = null;
		if (CourseConstants.TEACHER_KEY.equals(conflictType)) {
			groupByKey = "cpco_teacher_key";
		} else if (CourseConstants.CLASS_ROOM_KEY.equals(conflictType)) {
			groupByKey = "cpco_room_key";
		} else {
			throw  new ApiException("groupByKey 传参错误");
		}
		QueryWrapper<CoursePlanConflict> queryWrapper = new QueryWrapper<>();
		queryWrapper.select(groupByKey);
		// queryWrapper.eq("cpco_is_conflict", 0);
		if (keyList != null && keyList.size() != 0) {
			if (CourseConstants.TEACHER_KEY.equals(conflictType)) {
				queryWrapper.in("cpco_teacher_key", keyList);
			} else if (CourseConstants.CLASS_ROOM_KEY.equals(conflictType)) {
				queryWrapper.in("cpco_room_key", keyList);
			}
		}
		queryWrapper.and((mapper) -> {
			mapper.between("cour_begin_time", time, tetiTermEndDate);
		}).and((mapper) -> {
			mapper.between("cour_end_time", time, tetiTermEndDate);
		});
		queryWrapper.groupBy(groupByKey);
		queryWrapper.having("count(" + groupByKey + ")>1");
		List<Map<String, Object>> maps = this.baseMapper.selectMaps(queryWrapper);
		if (maps != null && maps.size() != 0) {
			for (Map<String, Object> map : maps) {
				String conflictKey = (String) map.get(groupByKey);
				if (CourseConstants.TEACHER_KEY.equals(conflictType)) {
					this.baseMapper.updateByTeacherConfict(conflictKey);
				} else if(CourseConstants.CLASS_ROOM_KEY.equals(conflictType)) {
					this.baseMapper.updateByClassRoomConfict(conflictKey);
				}
			}
		}
	}
	
	@Override
	public List<Long> selectIdByKeyList(List<String> allTeacherKeyList, List<String> allRoomKeyList) {
		return this.baseMapper.selectIdByKeyList(allTeacherKeyList, allRoomKeyList);
	}
	
	@Override
	public void updateToNotConflict(List<Long> idList) {
		this.baseMapper.updateToNotConflict(idList);
	}
}
